﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Subsonic.Resources;
using System.Collections.Generic;
using MichMan.Utilities.Requests;

namespace Subsonic.ViewModels
{
    public class PivotItemViewModel : BasePivotItemViewModel<ItemViewModel>
    {
    }

    public class BasePivotItemViewModel<T> : BasePivotItemViewModel where T : class
    {
        public BasePivotItemViewModel()
        {
            Items = new ObservableCollection<T>();
        }
        public ObservableCollection<T> Items { get; private set; }

        public sealed override IRequest Load()
        {
            if (Items.Count != 0)
            {
                return null;
            }

            IRequest request = LoadItems();

            if (request != null)
            {
                request.RunRequestCompleted += (s,e) => NotifyLoaded(e);
            }

            return request;
        }
    }

    public class BasePivotItemViewModel : BaseViewModel
    {
        // Load sub-Items collection.
        public virtual IRequest LoadItems()
        {
            return null;
        }

        private string pivotImage = null;
        public string PivotImage
        {
            get
            {
                return pivotImage;
            }
            set
            {
                if (value != pivotImage)
                {
                    pivotImage = value;
                    NotifyPropertyChanged("PivotImage");
                }
            }
        }

        private string title = null;
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                if (value != title)
                {
                    title = value.StaticResource();
                    NotifyPropertyChanged("Title");
                }
            }
        }

        public override string ToString()
        {
            return Title;
        }
    }

    public class Group<T> : ObservableCollection<T>
    {            
        public Group(string name, IEnumerable<T> items)
        {                
            this.Title = name;
            items.ToList().ForEach(i => Items.Add(i));
        }

        public override bool Equals(object obj)
        {                
            Group<T> that = obj as Group<T>;
            return (that != null) && (this.Title.Equals(that.Title));
        }

        public override int GetHashCode()
        {
            return Title.GetHashCode();
        }
        
        public string Title { get; set; }
    }
}
